<template>
  <div>
    <a-space class="mb-2 flex justify-end">
      <a-button type="primary" :disabled="selectedRowKeys.length <= 0" @click="onBatchTransferIn">批量转入</a-button>
    </a-space>
    <BaseTable
      :loading="loading"
      :data-source="tableData"
      :pagination="pagination"
      row-key="aptmLsh"
      :row-selection="{
        selectedRowKeys,
        getCheckboxProps: (record) => ({
          disabled: record.status != 1
        }),
        onChange: onSelectChange
      }"
      :scroll="{ x: 1800 }"
      fill
      column-code="000113"
    >
      <template #bodyCell="{ column, index, record }">
        <template v-if="column.dataIndex === 'xh'">
          <span>{{ index + 1 }}</span>
        </template>
        <template v-if="column.dataIndex === 'status'">
          <!-- 状态 0未安排 1已安排(待转入) 2作废 3待手术(已转入) 4手术中 5已手术(待转出) -->
          <a-tag v-if="record.status == 0">未安排</a-tag>
          <a-tag v-if="record.status == 1" color="blue">未转入</a-tag>
          <a-tag v-if="record.status == 2" color="red">作废</a-tag>
          <a-tag v-if="record.status == 3" color="green">已转入</a-tag>
          <a-tag v-if="record.status == 4" color="green">手术中</a-tag>
          <a-tag v-if="record.status == 5" color="blue">待转出</a-tag>
          <a-tag v-if="record.status == 6" color="green">已转出</a-tag>
        </template>
        <template v-if="column.dataIndex === 'oprPath'">
          <!-- 手术来源;0：门诊|1：住院 -->
          <span v-if="record.oprPath == 0">门诊</span>
          <span v-if="record.oprPath == 1">住院</span>
        </template>
        <template v-if="column.dataIndex === 'originalWardName'">
          <span>{{ record?.originalWardName }}</span>
        </template>
        <template v-if="column.dataIndex === 'addEmpName'">
          <span v-if="record.oprPath == 0">{{ record.intoOperationRecord.addEmpName }}</span>
          <EditCell v-else title="转运人">
            <SelectTable
              v-model:value="record.intoOperationRecord.addEmpName"
              :disabled="record.status != 1"
              type="员工"
              @select=";(record.intoOperationRecord.addEmpId = $event.empId), (record.intoOperationRecord.addEmpName = $event.empName)"
            />
          </EditCell>
        </template>
        <template v-if="column.dataIndex === 'createEmpName'">
          <span>{{ record?.intoOperationRecord?.createEmpName }}</span>
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          <span>{{ record?.intoOperationRecord?.createTime }}</span>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button v-if="record.status == 1" type="link" size="small" @click="transferIn(record)">转入</a-button>
          <BaseButton v-if="record.status == 3" confirm-text="确定撤销转入?" danger type="text" size="small" @click="cancelTransferIn(record)">
            撤销转入
          </BaseButton>
        </template>
      </template>
    </BaseTable>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'
import SelectTable from '@/components/SelectTable'

const { empName, empId } = useUserStore()
const emit = defineEmits(['refresh'])
const tableData = ref([])
const selectedRowKeys = ref([])
const props = defineProps({
  value: {
    type: Array,
    default: () => []
  },
  pagination: {
    type: Object,
    default: () => ({})
  },
  loading: {
    type: Boolean,
    default: () => false
  }
})
const transferIn = async (record) => {
  const { code, msg } = await post('oprOperation/opr/intoOperationRecord', { aptmLsh: record.aptmLsh, zyr: record?.intoOperationRecord?.addEmpId })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  emit('refresh')
}
const onBatchTransferIn = async () => {
  if (selectedRowKeys.value.length <= 0) return message.warning('请选择一项')
  const { code, msg } = await post('oprOperation/opr/intoOperationRecord', { aptmLshes: selectedRowKeys.value, zyr: '' })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  selectedRowKeys.value = []
  emit('refresh')
}

const cancelTransferIn = async (record) => {
  const { code, msg } = await post('oprOperation/opr/quashIntoOperationRecord', { aptmLsh: record.aptmLsh })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  emit('refresh')
}
const onSelectChange = (keys) => {
  selectedRowKeys.value = keys
}
watch(
  () => props.value,
  (val) => {
    if (val && val.length > 0) {
      tableData.value = JSON.parse(JSON.stringify(val))
      tableData.value.forEach((item) => {
        if (!item.intoOperationRecord) {
          item.intoOperationRecord = {}
          if (item.oprPath === 0) {
            item.intoOperationRecord.addEmpName = empName
            item.intoOperationRecord.addEmpId = empId
          }
        }
      })
    }
  },
  {
    deep: true
  }
)
</script>

<style lang="less" scoped></style>
